{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import io\n",
    "import pandas as pd\n",
    "import glob\n",
    "import os\n",
    "from shutil import move\n",
    "from os.path import join\n",
    "from os import listdir, rmdir\n",
    "\n",
    "target_folder = './tiny-imagenet-200/val/'\n",
    "\n",
    "val_dict = {}\n",
    "with open(target_folder + 'val_annotations.txt', 'r') as f:\n",
    "    for line in f.readlines():\n",
    "        split_line = line.split('\\t')\n",
    "        val_dict[split_line[0]] = split_line[1]\n",
    "        \n",
    "paths = glob.glob(target_folder + 'images/*')\n",
    "paths[0].split('/')[-1]\n",
    "for path in paths:\n",
    "    file = path.split('/')[-1]\n",
    "    folder = val_dict[file]\n",
    "    if not os.path.exists(target_folder + str(folder)):\n",
    "        os.mkdir(target_folder + str(folder))\n",
    "        \n",
    "for path in paths:\n",
    "    file = path.split('/')[-1]\n",
    "    folder = val_dict[file]\n",
    "    dest = target_folder + str(folder) + '/' + str(file)\n",
    "    move(path, dest)\n",
    "    \n",
    "os.remove('./tiny-imagenet-200/val/val_annotations.txt')\n",
    "rmdir('./tiny-imagenet-200/val/images')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
